# SerendipityImporter plugin for Movable Type
# Author: Robert Bohne
#
# $Id: RssReader.pm 6 2008-09-29 13:05:46Z Robert.Bohne $
package SerendipityImporter::RssReader;

use strict;
use warnings;

use Data::Dumper;
use LWP::UserAgent;
use XML::LibXML;
use List::MoreUtils qw(uniq);
use HTML::Entities;
use Carp qw/croak/; 

sub new{
  	my $class=shift;
	my $args =shift;
  	my $self={};

	unless (defined $args->{url}){
		croak 'Please set url!!';
	}
	
	$self->{url} = $args->{url} ;

	
	$self->{parser} = XML::LibXML->new;
	$self->{useragent} = LWP::UserAgent->new;
	
	if (defined $args->{proxy_server}) {
		$self->{useragent}->proxy(['http', 'ftp'], $args->{proxy_server});
	}
	
	
	$self->{request} = $self->{useragent}->request(HTTP::Request->new(GET => $self->{url}."/rss.php?nocache=1&all=1")) ;
	
	if ($self->{request}->is_error) {
		croak "\n\nError Code: ".$self->{request}->code."\nMessage:".$self->{request}->message."\n";
	}
	
	$self->{document} = $self->{parser}->parse_string($self->{request}->content);


	bless($self, $class);
  	return $self;
}

sub getAllAuthors{
	my $self = shift;

	my @authors = ();
	map {
		push @authors,$_->{_author};
	} @{$self->getAllEntries()};
	@authors = uniq @authors;
	return \@authors;
}


sub getAllCategories{
	my $self = shift;

	my @categories = ();
	foreach my $entry (@{$self->getAllEntries()}){
		push @categories, @{$entry->{_categories}};
	};

	@categories = uniq @categories;
	return \@categories;
}

sub getAllEntries{
	my $self = shift;
	
	return $self->{_entries} if (defined $self->{_entries});
	
	foreach my $element ($self->{document}->getElementsByTagName("item")){
		my $tmp = {};
		
		$tmp->{_title} = $element->getElementsByTagName('title')->string_value();

		$tmp->{_categories} = [];
		foreach my $el ($element->getElementsByTagName('category')){
			next unless length($el->string_value()) > 0;
			push @{$tmp->{_categories}}, $el->string_value();
		}

		($tmp->{_author}) = $element->getElementsByTagName('author')->string_value() =~ /.*\((.*)\).*/i;

		$tmp->{_content} = HTML::Entities::decode_entities($element->getElementsByTagName('content:encoded')->string_value());

		$tmp->{_pubDate} = $element->getElementsByTagName('pubDate')->string_value();

		# Comments stuff

		if ($element->getElementsByTagName('slash:comments')->string_value() > 0){
			my $request = $self->{useragent}->request(HTTP::Request->new(
				GET => HTML::Entities::decode_entities($element->getElementsByTagName('wfw:commentRss')->string_value()),
			));

			my $doc = $self->{parser}->parse_string($request->content);

			foreach my $el ($doc->getElementsByTagName("item")){
				my $comment = {
					_author => $el->getElementsByTagName('author')->string_value() =~ /.*\((.*)\).*/i,
					_pubDate => $el->getElementsByTagName('pubDate')->string_value(),
					_content => HTML::Entities::decode_entities($el->getElementsByTagName('content:encoded')->string_value()),
				};

				push @{$tmp->{_comments}},$comment;
			}
		}
		
		push @{$self->{_entries}},$tmp;
	}
	
	return $self->{_entries};

}


1;
